﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Example10.aspx.cs" Inherits="Chapter29.Example10" %>

<!DOCTYPE html>

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>代码清单 29-10 使用 CSSPrimitiveValue 对象</title>
    <meta name="author" content="Adam Freeman" />
    <meta name="description" content="A simple example" />
    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" />
    <style>
        p {
            color: white;
            background-color: gray;
            padding: 7px !important;
        }

        table {
            border: thin solid black;
            border-collapse: collapse;
            margin: 5px;
            float: left;
        }

        td {
            padding: 2px;
        }
    </style>
</head>
<body>
    <p id="block1">
        There are lots of different kinds of fruit - there are you over 500 varieties 
        of banana alone. By the time we add the countless 
        types of apples, oranges, and other well-known fruit, we are 
        faced with thousands of choices.
    </p>
    <div id="placeholder"></div>
    <script>
        var placeholder = document.getElementById("placeholder");
        displayStyles();

        function displayStyles() {
            var newElem = document.createElement("table");
            newElem.setAttribute("border", "1");

            var style = document.styleSheets[0].cssRules[0].style;

            for (var i = 0; i < style.length; i++) {
                var val = style.getPropertyCSSValue(style[i]);

                if (val.primitiveType == CSSPrimitiveValue.CSS_PX) {
                    addRow(newElem, style[i], val.getFloatValue(CSSPrimitiveValue.CSS_PX), "pixels");
                    addRow(newElem, style[i], val.getFloatValue(CSSPrimitiveValue.CSS_PT), "points");
                    addRow(newElem, style[i], val.getFloatValue(CSSPrimitiveValue.CSS_IN), "inches");
                } else if (val.primitiveType == CSSPrimitiveValue.CSS_RGBCOLOR) {
                    var color = val.getRGBColorValue();
                    addRow(newElem, style[i], color.red.cssText + " " + color.green.cssText + " " + color.blue.cssText, "(color)");
                } else {
                    addRow(newElem, style[i], val.cssText, "(other)");
                }
            }

            placeholder.appendChild(newElem);
        }

        function addRow(elem, header, value) {
            elem.innerHTML += "<tr><td>" + header + ":</td><td>" + value + "</td></tr>";
        }
    </script>
</body>
</html>
